package com.java.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.endpoint.Client;  
import com.java.entity.User;  
import com.java.service.IUserService;  
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import org.w3c.dom.Document;

public class MainClient2 {  
	// 命名空间
    private static String namespace = "http://service.java.com/";
    // 前缀
    private static String prefix = "ns1";
    // 服务的发布地址wsdl
    private static String wsdlUrl = "http://127.1.1.0:8080/SpringCxfServer/user?wsdl";
    // 服务的发布名称
    private static String wsdl_serviceName = "IUserServiceService";
    // 服务的端口 
    private static String wsdl_portName = "IUserServicePort";
    //localPart
    private static String local_part = "sayHi";
 
    public static void main(String[] args) throws Exception {  
    	try {
    		//创建服务
            URL url = new URL(wsdlUrl);
            QName serviceName = new QName(namespace, wsdl_serviceName);
            QName portName = new QName(namespace, wsdl_portName);
            Service service = Service.create(url, serviceName);
            //创建dispatch
            Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);

            //创建SOAPMessage
            MessageFactory mf = MessageFactory.newInstance();
            SOAPMessage msg = mf.createMessage();
            SOAPPart sp = msg.getSOAPPart();
            SOAPEnvelope envelope = sp.getEnvelope();

            //header
            /*
            SOAPHeader header =envelope.getHeader();
            if (null == header){
                header = envelope.addHeader();
            }
            QName hname1 = new QName(namespace, "Address", prefix);
            header.addChildElement(hname1).setValue("request_address");
            */
            
            //body
            SOAPBody body = envelope.getBody();
            QName bname = new QName(namespace, local_part, prefix);
            SOAPBodyElement bodyElement = body.addBodyElement(bname);
            bodyElement.addChildElement("text").setValue("1234");
            //通过dispatch传递消息
            SOAPMessage reply = dispatch.invoke(msg);
            System.out.println("reply:");
            reply.writeTo(System.out);
            System.out.println();
            System.out.println("************************");
            // reply中返回码进行验证, 响应消息处理,将响应的消息转换为dom对象  
//            Document doc = reply.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();  
//            String strRetCode = doc.getElementsByTagName("RetCode").item(0).getTextContent();
//            System.out.println("strRetCode:"+strRetCode);  
//            String strRetMsg = doc.getElementsByTagName("RetMsg").item(0).getTextContent();
//            System.out.println("strRetMsg:"+strRetMsg);  
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    	
        
    }  
}  
